import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-27
 * Time: 22:28
 */
public class SingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public int ListSize;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        ListSize++;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);

        if (head == null){
            head = node;
            ListSize++;
            return;
        }

        ListNode cur = head;
        while (cur != null){
            cur = cur.next;
        }
        cur.next = node;
        ListSize++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if (index<0||index>ListSize){
            return false;
        }

        if (index == 0){
            addFirst(data);
            return true;
        }
        if (index == ListSize){
            addLast(data);
            return true;
        }

        ListNode cur = head;
        ListNode node = new ListNode(data);
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        node.next = cur.next;
        cur.next = node;
        ListSize++;
        return true;

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            ListSize--;
            return;
        }

        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                cur.next = cur.next.next;
                ListSize--;
                return;
            }
            cur = cur.next;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                cur.next = cur.next.next;
                ListSize--;
            }else {
                cur = cur.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        return ListSize;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = this.head;
        ListNode curNext = null;
        while (cur != null){
            curNext = cur.next;
            cur.next= null;
            cur = curNext;
        }
        head = null;
    }

    public int kthToLast(int k) {
        if (k <= 0){
            return -1;
        }

        ListNode fast = head;
        ListNode slow = head;

        int count = 0;
        while (count != k-1){
            fast = fast.next;
            if (fast == null){
                return -1;
            }
            count++;
        }

        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return null;
        }

        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }

        ListNode cur = head.next;
        head.next = null;

        while(cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }

        return head;

    }
}
